using System;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseManager.BO;

namespace UseCaseManager.UI
{
    /// <summary>
    /// A parent-child grid control used for adding and editing Packages and their UseCases.
    /// </summary>
    public class PackagesAndUseCasesControl : UserControlWin, IFormControl
    {
        private readonly IControlFactory _controlFactory;

        public PackagesAndUseCasesControl(IControlFactory controlFactory)
        {
            _controlFactory = controlFactory;

            IGridControl packagesGridControl = SetupParentControl();
            IGridControl useCasesGridControl = SetupChildControl();
            
            // Add the grids to the control. The Packages grid fills the top part of the control.  
            // Change the size here depending on how many items you want to see in this grid.  
            // The UseCases grid fills the rest of the control.
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            packagesGridControl.Height = 200;
            layoutManager.AddControl(packagesGridControl, BorderLayoutManager.Position.North);
            layoutManager.AddControl(useCasesGridControl, BorderLayoutManager.Position.Centre, true);
            
            // Connect the Packages and UseCases grids so that when an item is selected
            // in the Packages grid, the correct UseCases are shown in the child grid.
            packagesGridControl.Grid.BusinessObjectSelected +=
                 delegate
                 {
                     try
                     {
                         Package selectedPackage = packagesGridControl.Grid.SelectedBusinessObject as Package;
                         if (selectedPackage == null) return;
                         useCasesGridControl.BusinessObjectCollection = selectedPackage.UseCases;
                     }
                     catch (Exception ex)
                     {
                         GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                     }
                 };
        }

        private IGridControl SetupParentControl()
        {
            // Create the Packages read-only grid
            IGridControl packagesGridControl = _controlFactory.CreateReadOnlyGridControl();

            // Used to specify a custom UI def
            packagesGridControl.Initialise(ClassDef.Get<Package>(), "default");

            // Load the collection of Packages.  You can set the criteria and order by values here.
            BusinessObjectCollection<Package> packagesCollection = Broker.GetBusinessObjectCollection<Package>("", "");
            packagesGridControl.BusinessObjectCollection = packagesCollection;

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            packagesGridControl.FilterControl.Visible = false;
            //packagesGridControl.FilterControl.AddStringFilterTextBox("", "");

            return packagesGridControl;
        }

        private IGridControl SetupChildControl()
        {
            // Create the UseCases read-only grid to show the child items
            IGridControl useCasesGridControl = _controlFactory.CreateEditableGridControl();

            // Used to specify a custom UI def
            useCasesGridControl.Initialise(ClassDef.Get<UseCase>(), "DUI");

            // Creates an empty collection of UseCases to put in the child grid
            BusinessObjectCollection<UseCase> t2Collection = new BusinessObjectCollection<UseCase>();
            useCasesGridControl.BusinessObjectCollection = t2Collection;

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            useCasesGridControl.FilterControl.Visible = false;
            //useCasesGridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //useCasesGridControl.Buttons.ShowDefaultDeleteButton = true;
            return useCasesGridControl;
        }
        
        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form) { }
    }
}
